import cv2
import numpy as np
import torch
import torch.nn.functional as F

# 读取图像
img = cv2.imread('image.jpg')

# 1. 最近邻插值
upsampled_nn = cv2.resize(img, (512, 512), interpolation=cv2.INTER_NEAREST)

# 2. 双线性插值
upsampled_bilinear = cv2.resize(img, (512, 512), interpolation=cv2.INTER_LINEAR)

# 3. PyTorch 实现转置卷积
input_tensor = torch.randn(1, 1, 64, 64)  # [batch, channels, height, width]
upsampled_transconv = F.conv_transpose2d(input_tensor, torch.ones(1, 1, 3, 3), stride=2, padding=1)

# 显示图像
cv2.imshow("Upsampled Nearest", upsampled_nn)
cv2.imshow("Upsampled Bilinear", upsampled_bilinear)
cv2.waitKey(0)
cv2.destroyAllWindows()
